/*
 * @Author: ziqi jhzq12345678
 * @Date: 2024-08-20 09:55:00
 * @LastEditors: wangdazhuang
 * @LastEditTime: 2025-01-27 20:31:44
 * @FilePath: /pornhub_app/lib/views/search/result/search_result_page.dart
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
import 'package:pornhub_app/components/app_bar/app_bar_view.dart';
import 'package:pornhub_app/components/tab_bar_indicator/easy_fixed_indicator.dart';
import 'package:pornhub_app/utils/extension.dart';
import 'package:pornhub_app/views/search/result/search_topic_result.dart';
import 'package:pornhub_app/views/search/result/search_user_result.dart';
import 'package:pornhub_app/views/search/result/search_dynamic_result.dart';
import 'package:pornhub_app/views/search/result/search_short_result.dart';
import 'package:pornhub_app/views/search/search_controller.dart';
import 'package:pornhub_app/views/search/result/search_video_result.dart';
import 'package:pornhub_app/utils/color.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';

class SearchResultPage extends StatefulWidget {
  const SearchResultPage({super.key});

  @override
  State createState() => _SearchResultPage();
}

class _SearchResultPage extends State<SearchResultPage>
    with SingleTickerProviderStateMixin {
  TextEditingController searchTextController = TextEditingController();

  final searchvc = Get.find<SearchPageController>();

  late TabController tabController;

  final _tabs = [
    {
      'name': '视频',
      'type': 1,
    },
    {
      'name': '刷片',
      'type': 2,
    },
    {
      'name': '用户',
      'type': 3,
    },
    {
      'name': '话题圈',
      'type': 4,
    },
    {
      'name': 'G圈',
      'type': 5,
    },
  ];
  var activeTab = 0;

  void handleSearch([String? v]) {
    if (searchTextController.text.isEmpty && v == null) return;
    String words = v ?? searchTextController.text;
    searchvc.addHistory(words);
  }

  @override
  void initState() {
    searchTextController.text = Get.arguments['word'];

    tabController = TabController(
      length: _tabs.length,
      vsync: this,
    )..addListener(() {
        if (tabController.index.toDouble() == tabController.animation?.value) {
          activeTab = tabController.index;
        }
      });
    super.initState();
  }

  @override
  void dispose() {
    tabController.dispose();
    super.dispose();
  }

  initListWidget(type) {
    switch (type) {
      case 1:
        return SearchVideoResult(searchType: type);
      case 2:
        return SearchShortResult(
          searchType: type,
        );
      case 3:
        return SearchUserResult(searchType: type);
      case 4:
        return SearchTopicResult(
          searchType: type,
        );
      default:
        return SearchDynamicResult(
          searchType: type,
        );
    }
  }

  @override
  Widget build(BuildContext context) {
    return DefaultTabController(
        length: _tabs.length,
        child: Scaffold(
          appBar: AppBar(
            elevation: 0,
            automaticallyImplyLeading: false,
            titleSpacing: 0,
            title: Flex(
              direction: Axis.horizontal,
              children: [
                IconButton(
                  padding: EdgeInsets.only(left: 5.w),
                  iconSize: 24.w,
                  icon: Icon(
                    Icons.arrow_back_ios,
                    color: COLOR.hexColor('#ffffff'),
                  ),
                  onPressed: () => Navigator.pop(context),
                ),
                Expanded(
                    child: Container(
                        width: 296.w,
                        height: 30.w,
                        clipBehavior: Clip.hardEdge,
                        alignment: Alignment.center,
                        margin: EdgeInsets.only(right: 20.w),
                        padding: EdgeInsets.symmetric(horizontal: 15.w),
                        decoration: BoxDecoration(
                          border: Border.all(color: Colors.white),
                          borderRadius: BorderRadius.circular(15.w),
                        ),
                        child: Flex(
                          direction: Axis.horizontal,
                          mainAxisSize: MainAxisSize.min,
                          children: [
                            Image.asset(
                              'assets/search/ic_search.png',
                              width: 16.w,
                              height: 16.w,
                            ),
                            Padding(padding: EdgeInsets.only(left: 8.w)),
                            Expanded(
                                child: TextField(
                              style: TextStyle(
                                  color: Colors.white, fontSize: 14.w),
                              controller: searchTextController,
                              onSubmitted: (value) {
                                searchvc.addHistory(value);
                                handleSearch(value);
                              },
                              decoration: InputDecoration(
                                  isCollapsed: true,
                                  hintText: "输入搜索内容",
                                  hintStyle: TextStyle(
                                      color: COLOR.hexColor('#898A8E'),
                                      fontSize: 14.w),
                                  enabledBorder: const UnderlineInputBorder(
                                      borderSide: BorderSide(
                                          color: Color.fromRGBO(0, 0, 0, 0))),
                                  focusedBorder: const UnderlineInputBorder(
                                      borderSide: BorderSide(
                                          color: Color.fromRGBO(0, 0, 0, 0)))),
                            ))
                          ],
                        ))),
                GestureDetector(
                  onTap: handleSearch,
                  child: Text(
                    '搜索',
                    style: TextStyle(
                        color: COLOR.hexColor('#B93FFF'), fontSize: 15.w),
                  ),
                ).paddingRight(14.w)
              ],
            ),
            bottom: PreferredSize(
              preferredSize: const Size.fromHeight(kToolbarHeight),
              child: Column(
                children: [
                  TabBar(
                      tabAlignment: TabAlignment.fill,
                      isScrollable: false,
                      labelColor: COLOR.hexColor('#B93FFF'),
                      unselectedLabelColor: COLOR.hexColor('#808080'),
                      indicatorColor: COLOR.hexColor('#B93FFF'),
                      labelStyle: TextStyle(
                          fontSize: 14.w, fontWeight: FontWeight.w600),
                      unselectedLabelStyle: TextStyle(
                          fontSize: 14.w, fontWeight: FontWeight.w500),
                      dividerColor: Colors.transparent,
                      controller: tabController,
                      indicator: EasyFixedIndicator(
                        color: COLOR.hexColor('#B93FFF'),
                        width: 10.w,
                        height: 3.w,
                      ),
                      tabs:
                          _tabs.map((v) => Tab(text: '${v['name']}')).toList()),
                  Container(
                    height: 1.w,
                    color: COLOR.hexColor('#292A31'),
                  )
                ],
              ),
            ),
          ),
          body: TabBarView(
            controller: tabController,
            children: _tabs.map((v) {
              return Container(
                  margin: EdgeInsets.only(top: 10.w),
                  child: initListWidget(v['type']));
            }).toList(),
          ),
        ));
  }
}
